package com.pat.java.base.http;

import java.io.IOException;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.WindowConstants;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

public class DisplayHtml {

	@SuppressWarnings("deprecation")
	public static void showNetPage(String str) {

		JEditorPane jep = new JEditorPane();
		jep.setEditable(false);
		jep.addHyperlinkListener(new LinkFollower(jep));
		try {
			jep.setPage(str);
		} catch (IOException e) {
			jep.setContentType("text/html");
			jep.setText("<html>Could not load " + str + " </html>");
		}

		JScrollPane jscrollp = new JScrollPane(jep);
		JFrame f = new JFrame("西安交通大学主页");
		f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		f.setContentPane(jscrollp);
		f.setSize(600, 400);
		f.show();
	}

	public static void main(String[] args) {
		String getURL = "http://www.google.com";
		showNetPage(getURL);
	}
}

class LinkFollower implements HyperlinkListener {
	private JEditorPane jep;

	public LinkFollower(JEditorPane jep) {
		this.jep = jep;
	}

	public void hyperlinkUpdate(HyperlinkEvent evt) {

		if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {

			try {

				jep.setPage(evt.getURL());
			} catch (Exception e) {
				System.out.println(e.getMessage());
			}
		}
	}
}
